package flex.cc.basicinformation.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;

import flex.cc.PublicUtil;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.basicinformation.entity.ServiceConfigEntity;
import flex.cc.basicinformation.service.ServiceConfigService;
import flex.cc.core.support.BaseController;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @author WANGCONG
 * 2022年2月23日下午15:02:09
 * @desc 服务项目配置控制层
 */
@RestController
@RequestMapping(value = "/basic/serviceConfig")
public class ServiceConfigController extends BaseController{
	
	@Resource
	private ServiceConfigService serviceConfigService;

	/**
	 *
	 * @author WANGCONG
	 * 2022年2月23日下午16:35:06
	 * @desc 列表分页查询
	 */
	@PostMapping(value = "/queryListWithPage")
    @ApiOperation(httpMethod = "POST", value = "查询服务项目配置列表")
    public Wrapper<IPage<ServiceConfigEntity>> queryOrderListWithPage(@ApiParam(name = "configMain", value = "查询信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("查询服务项目配置列表configMain={}", reqVo);
        ServiceConfigEntity vo = new ServiceConfigEntity();
        vo.setServiceCode( PublicUtil.isNotEmpty(reqVo.get("serviceCode")) ? reqVo.get("serviceCode").toString() : null );//服务项目编码
        vo.setServiceName( PublicUtil.isNotEmpty(reqVo.get("serviceName")) ? reqVo.get("serviceName").toString() : null );//服务项目名称
        vo.setServiceId( PublicUtil.isNotEmpty(reqVo.get("serviceId")) ? reqVo.get("serviceId").toString() : null );//服务商
        vo.setServiceType( PublicUtil.isNotEmpty(reqVo.get("serviceType")) ? reqVo.get("serviceType").toString() : null );//服务类型
        vo.setStartTime( PublicUtil.isNotEmpty(reqVo.get("startTime")) ? reqVo.get("startTime").toString() : null ); //起止时间
        vo.setEndTime( PublicUtil.isNotEmpty(reqVo.get("endTime")) ? reqVo.get("endTime").toString() : null );
        vo.setPageNum( Integer.parseInt(String.valueOf(reqVo.get("pageNum"))) );  //分页
        vo.setPageSize( Integer.parseInt(String.valueOf(reqVo.get("pageSize"))) );
        IPage<ServiceConfigEntity> page = serviceConfigService.queryListWithPage(vo);
        return WrapMapper.ok(page);
    }
	
	/**
	 *
	 * @author WANGCONG
	 * 2022年2月23日下午17:37:29
	 * @desc 切换状态
	 */
   @PostMapping(value = "/modifyStatus")
   @ApiOperation(httpMethod = "POST", value = "切换服务项目配置状态")
   public Wrapper<Boolean> modifyStatus(@ApiParam(name = "modifyStatus", value = "服务项目状态启用/禁用")
                                                @RequestBody ServiceConfigEntity configDto) {
       logger.info("根据dataId切换服务项目状态configDto={}", configDto);
       LoginAuthDto loginAuthDto = getLoginAuthDto();
       boolean result = serviceConfigService.modifyStatus(configDto, loginAuthDto);
       return handleResult(result);
   }
   
   /**
	 *
	 * @author WANGCONG
	 * 2022年2月24日下午13:16:28
	 * @desc 详情查看
	 */
   @PostMapping(value = "/detail/{dataId}")
   @ResponseBody
   @ApiOperation(httpMethod = "POST", value = "服务项目配置详情")
   public Wrapper<String> detail(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable String dataId) {
       logger.info("服务项目配置详情 -- detail() ====> ", dataId);
       Map<String, Object> result = new HashMap<String, Object>();
       if (PublicUtil.isNotEmpty(dataId)){
    	   ServiceConfigEntity config = serviceConfigService.queryByDataId(dataId);
    	   config.setDataId(config.getId()+"");
    	   result.put("responseCode", "00");
    	   result.put("responseData", config);
    	   return WrapMapper.ok(JSON.toJSONString(result));
       }
       return WrapMapper.ok();
   }
   
   /**
	 *
	 * @author WANGCONG
	 * 2022年2月24日下午13:46:50
	 * @desc 服务项目配置保存
	 */
   @PostMapping(value = "/save")
   @ResponseBody
   @ApiOperation(httpMethod = "POST", value = "服务项目配置保存")
   public Wrapper<String> save(@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo) {
       logger.info("服务项目配置保存reqVo={}", reqVo);
       serviceConfigService.save(reqVo);
       return WrapMapper.ok();
   }
   
   /**
	 *
	 * @author WANGCONG
	 * 2022年2月24日下午15:02:16
	 * @desc 唯一性校验：同一个服务商下，相同服务类型中，不能有重复的服务项目编码
	 */
   @PostMapping(value = "/uniqueCheck/{dataId}/{serviceId}/{serviceType}/{serviceCode}")
   @ResponseBody
   @ApiOperation(httpMethod = "POST", value = "唯一性校验")
   public Wrapper<String> uniqueCheck(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable String dataId,
		   @PathVariable String serviceId, @PathVariable String serviceType, @PathVariable String serviceCode) {
       logger.info("服务项目配置保存前唯一性校验");
       Map<String,Object> result = new HashMap<String,Object>();
       Map<String, Object> params = new HashMap<String, Object>();
       params.put("dataId", dataId);
       params.put("serviceId", serviceId);
//       params.put("serviceType", serviceType);
       params.put("serviceCode", serviceCode);
       boolean b = serviceConfigService.uniqueCheck(params);
       if(b){
    	   result.put("responseCode", "00");
    	   result.put("responseMsg", "唯一性校验成功");
       }else{
    	   result.put("responseCode", "01");
    	   result.put("responseMsg", "当前服务项目编码已存在！");
       }
       return WrapMapper.ok(JSON.toJSONString(result));
   }
   
}
